package Greedy;

public class 加油站 {
    public int canCompleteCircuit(int[] gas, int[] cost) {
        int aftSum = 0;
        int start = 0;
        int max = Integer.MIN_VALUE;
        for (int i = gas.length - 1; i >= 0; i--) {
            aftSum += gas[i] - cost[i];
            if (aftSum > max) {
                start = i;
                max = aftSum;
            }
        }
        if (aftSum < 0) return -1;
        else return start;
    }
}
